package org.dromara.web.controller;


import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.MD5;
import com.alibaba.fastjson.JSONObject;
import com.uduncloud.sdk.client.UdunClient;
import org.apache.commons.codec.digest.Md5Crypt;
import org.dromara.web.domain.UdunNotifyReq;
import org.dromara.web.plugins.udun.UDUNWalletService;
import org.dromara.web.service.business.UDunService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import software.amazon.awssdk.utils.Md5Utils;

@RestController
@RequestMapping("/udun")
public class UDUNWalletController {

    @Autowired
    UDunService uDunService;


    @SaIgnore
    @PostMapping("/notify")
    public String walletNotify(UdunNotifyReq req) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(req.getBody())
            .append(UDUNWalletService.UDUN_API_KEY)
            .append(req.getNonce())
            .append(req.getTimestamp());
        String sign = SecureUtil.md5(stringBuilder.toString()).toLowerCase();
        if (!sign.equals(req.getSign())) {
            return "ERROR";
        }

        JSONObject jsonObject = JSONObject.parseObject(req.getBody());
        if (1 == jsonObject.getInteger("tradeType")) {
            // 充值
            uDunService.udunRechargeNotify(jsonObject.getString("address"), jsonObject.getBigDecimal("amount"));
        }


        return "SUCCESS";
    }
}
